package com.boyadegroot.divelog.ui.diver.tabs;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.TextView;

import com.boyadegroot.divelog.R;
import com.boyadegroot.divelog.data.DiveLogData;
import com.boyadegroot.divelog.data.DiveLogDatabaseContract;
import com.boyadegroot.divelog.models.DiveCourse;
import com.boyadegroot.divelog.models.DiveOrganization;
import com.boyadegroot.divelog.utilities.DateUtils;

public class DiverCertsAdapter extends SimpleCursorAdapter {

    public DiverCertsAdapter(Context context, int layout) {
        super(context, layout, null, new String[0], new int[0], 0);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Long courseId = cursor.getLong(cursor.getColumnIndex(DiveLogDatabaseContract.DiveCertifications.COLUMN_NAME_COURSE_ID));
        DiveLogData data = DiveLogData.getInstance(context);
        DiveCourse course = data.getDiveCourse(courseId);
        DiveOrganization organization = course.getOrganization();
        long date = cursor.getLong(cursor.getColumnIndex(DiveLogDatabaseContract.DiveCertifications.COLUMN_NAME_DATE));
        String number = cursor.getString(cursor.getColumnIndex(DiveLogDatabaseContract.DiveCertifications.COLUMN_NAME_NUMBER));
        TextView courseView = (TextView) view.findViewById(R.id.course);
        TextView dateView = (TextView) view.findViewById(R.id.date);
        TextView numberView = (TextView) view.findViewById(R.id.number);
        courseView.setText(String.format("%s %s", organization.getName(), course.getName()));
        numberView.setText(number);
        dateView.setText(DateUtils.getLongDateString(date));
        super.bindView(view, context, cursor);
    }
}
